<?php

namespace App\Helper;

use App\Enums\Notification\CounterApply;
use App\Enums\Notification\CounterNotify;
use App\Enums\Notification\GameNotify;
use App\Enums\Notification\ReserveNotify;
use App\Models\User;

class NotificationRulesHelper
{
    public static function getRules(User|null $user = null)
    {
        $counterApply = [
            'value' => 'counter_apply',
            'label' => '收银申请',
            'children' => [],
            'selected' => true,
        ];
        foreach (CounterApply::cases() as $case) {

            $selected = $user && $user->notificationRules()->where([
                    'type' => 'counter_apply',
                    'notification_name' => $case->value
                ])->exists();

            $counterApply['children'][] = [
                'value' => $case->value,
                'label' => $case->label(),
                'selected' => $selected
            ];

            if (!$selected)
                $counterApply['selected'] = false;
        }

        $counterNotify = [
            'value' => 'counter_notify',
            'label' => '收银通知',
            'children' => [],
            'selected' => true,
        ];
        foreach (CounterNotify::cases() as $case) {

            $selected = $user && $user->notificationRules()->where([
                    'type' => 'counter_notify',
                    'notification_name' => $case->value
                ])->exists();

            $counterNotify['children'][] = [
                'value' => $case->value,
                'label' => $case->label(),
                'selected' => $selected
            ];

            if (!$selected)
                $counterNotify['selected'] = false;
        }

        $gameNotify = [
            'value' => 'game_notify',
            'label' => '比赛通知',
            'children' => [],
            'selected' => true,
        ];
        foreach (GameNotify::cases() as $case) {

            $selected = $user && $user->notificationRules()->where([
                    'type' => 'game_notify',
                    'notification_name' => $case->value
                ])->exists();
            $gameNotify['children'][] = [
                'value' => $case->value,
                'label' => $case->label(),
                'selected' => $selected
            ];

            if (!$selected)
                $gameNotify['selected'] = false;
        }

        $reserveNotify = [
            'value' => 'reserve_notify',
            'label' => '预约通知',
            'children' => [],
            'selected' => true,
        ];
        foreach (ReserveNotify::cases() as $case) {

            $selected = $user && $user->notificationRules()->where([
                    'type' => 'reserve_notify',
                    'notification_name' => $case->value
                ])->exists();
            $reserveNotify['children'][] = [
                'value' => $case->value,
                'label' => $case->label(),
                'selected' => $selected
            ];

            if (!$selected)
                $reserveNotify['selected'] = false;
        }

        $res = [
            $counterApply,
            $counterNotify,
            $gameNotify,
            $reserveNotify
        ];

        return $res;
    }
}
